package com.sniff.atp.constant.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @BelongsProject: sniff-atp
 * @BelongsPackage: com.sniff.atp.constant.config
 * @Author: zxy
 * @CreateTime: 2023-03-23 19:57
 * @UpdateTime: 2023-03-27 15:53
 * @Description: 配置跨域问题
 * @Version: 1.0
 */
@Configuration
public class CrosConfig {
    // 当前跨域请求最大有效时长。这里默认1天 24 * 60 * 60
    private static final long MAX_AGE = 86400;

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 1 设置访问源地址
        corsConfiguration.addAllowedOrigin("*");
        // 2 设置访问源请求头
        corsConfiguration.addAllowedHeader("*");
        // 3 设置访问源请求方法
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setMaxAge(MAX_AGE);
        // 4 对接口配置跨域设置
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(source);
    }
}
